package com.ccf.business.etl.model;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.annotation.TableField;
import com.component.api.constants.ComponentCodeConstants;
import com.component.api.model.param.ComponentDefine;
import com.component.api.model.param.ComponentProperties;
import com.component.api.model.param.InParam;
import com.component.api.model.param.Port;
import com.service.mybatis.pojo.SuperEntity;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 组件表
 *
 * @author ccf
 * @date 2021-11-25 15:36:06
 */
@Getter
@Setter
@EqualsAndHashCode(callSuper = false)
@TableName("wj_component")
@ApiModel(value="wj_component对象", description="组件表")
@NoArgsConstructor
@AllArgsConstructor
public class Component extends SuperEntity {
    private static final long serialVersionUID=1L;


    @ApiModelProperty(value = "组件名称")
    private String name;

    @ApiModelProperty(value = "组件编码")
    private String code;

    @ApiModelProperty(value = "组件组id")
    private String pid;

    @ApiModelProperty(value = "组件排序")
    private Integer sort;

    @ApiModelProperty(value = "组件依赖类")
    private String className;

    @ApiModelProperty(value = "组件类型")
    private Integer type;

    @ApiModelProperty(value = "组件运行平台")
    private String platform;

    @ApiModelProperty(value = "组件icon")
    private String icon;

    @ApiModelProperty(value = "组件右键")
    private String rightClick;

    @ApiModelProperty(value = "右键参数")
    private String params;

    @ApiModelProperty(value = "端口信息")
    private String ports;

    @ApiModelProperty(value = "参数信息")
    @TableField(exist = false)
    private List<InParam> paramList;

    @ApiModelProperty(value = "端口信息")
    @TableField(exist = false)
    private List<Port> portList;

    /**
     * 等于输入端口数
     */
    @ApiModelProperty(value = "输入连线数")
    @TableField(exist = false)
    private Integer enterLine;

    /**
     * 这个和输出端口不一样，表示能拉多少个线出来
     */
    @ApiModelProperty(value = "输出连线数")
    @TableField(exist = false)
    private Integer outputLine;

    @ApiModelProperty(value = "是否需要获取输入节点信息")
    @TableField(exist = false)
    private Boolean getPrevNodes;

    public Component(ComponentProperties properties, ComponentDefine componentDefine) {
        this.setId(properties.getId());
        this.name = properties.getZhName();
        this.code = properties.getCode();
        this.pid = properties.getGroupId();
        this.sort = properties.getSort();
        this.className = properties.getClassName();
        this.type = properties.getType();
        this.platform = properties.getPlatform();
        this.icon = properties.getIcon();
        this.rightClick = StrUtil.join( "," , properties.getRightClicks());
        if (componentDefine != null) {
            this.params = JSON.toJSONString(componentDefine.getInParams());
            this.ports = JSON.toJSONString(componentDefine.getPorts());
        }
    }

    public List<InParam> getParamList() {
        if (StrUtil.isNotBlank(params)){
            return JSON.parseArray(params, InParam.class);
        }
        return paramList;
    }

    public List<Port> getPortList() {
        if (StrUtil.isNotBlank(ports)){
            List<Port> ports = JSON.parseArray(this.ports, Port.class);
            Map<Integer, List<Port>> typeMap = ports.stream().collect(Collectors.groupingBy(e -> e.getType()));
            List<Port> in = typeMap.get(Port.IN);
            this.enterLine = in == null?0: in.size();
            List<Port> out =typeMap.get(Port.OUT);
            this.outputLine = out == null?0: out.size();;
            return ports;
        }
        return portList;
    }

    public Integer getEnterLine() {
        if (enterLine == null){
            getPortList();
        }
        return enterLine;
    }

    /**
     * 目前输出线 要么0 要么 -1
     *  0表示没有输出 如数据输出 -1表示可以无限拉线
     * @return
     */
    public Integer getOutputLine() {
        if (outputLine == null){
            getPortList();
        }
        return outputLine;
    }

    /**
     * 是否需要查询输出节点
     *  目前只有数据输出，汇聚合并 返回true
     * @return
     */
    public Boolean getGetPrevNodes() {
        if  (ComponentCodeConstants.DATA_OUTPUT.equalsIgnoreCase(code) ||
                ComponentCodeConstants.GATHER_MERGE.equalsIgnoreCase(code)
        ){
            return true;
        }
        return false;
    }


}
